package cn.schoolwow.quickdao.test.module.business.user.dql.general.testcase;

import cn.schoolwow.quickdao.domain.DAO;
import cn.schoolwow.quickdao.test.entity.User;
import cn.schoolwow.quickdao.test.module.business.user.dml.user.testcase.AddUserTestCaseFlow;
import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;
import org.junit.Assert;

import java.util.List;

public class FetchAndExistUserTestCaseFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        flowContext.executeFlowList(new AddUserTestCaseFlow());
        fetch(flowContext);
        fetchList(flowContext);
        exist(flowContext);
        existAny(flowContext);
        existAll(flowContext);
    }

    @Override
    public String name() {
        return "查询用户信息-fetch";
    }

    private void fetch(FlowContext flowContext){
        DAO dao = flowContext.checkInstanceData(DAO.class);
        User insertUser = flowContext.checkData("insertUser", User.class);

        {
            User user = dao.fetch(User.class, insertUser.getId());
            Assert.assertEquals(insertUser.getUsername(), user.getUsername());
        }
        {
            User user = dao.fetch(User.class, "username", insertUser.getUsername());
            Assert.assertEquals(insertUser.getUsername(), user.getUsername());
        }
    }

    private void fetchList(FlowContext flowContext){
        DAO dao = flowContext.checkInstanceData(DAO.class);
        List<User> insertUserList = flowContext.checkData("insertUserList", List.class);

        String password = insertUserList.get(0).getPassword();

        List<User> userList = dao.fetchList(User.class, "password", password);
        Assert.assertEquals(11, userList.size());
    }

    private void exist(FlowContext flowContext){
        DAO dao = flowContext.checkInstanceData(DAO.class);
        User insertUser = flowContext.checkData("insertUser", User.class);

        {
            boolean exist = dao.exist(insertUser);
            Assert.assertTrue(exist);
        }
        {
            boolean exist = dao.uniqueFieldNames("username").exist(insertUser);
            Assert.assertTrue(exist);
        }
        {
            boolean exist = dao.uniqueFieldNames("username", "password").exist(insertUser);
            Assert.assertTrue(exist);
        }
    }

    private void existAny(FlowContext flowContext){
        DAO dao = flowContext.checkInstanceData(DAO.class);
        List<User> insertUserList = flowContext.checkData("insertUserList", List.class);

        {
            boolean exist = dao.existAny(insertUserList);
            Assert.assertTrue(exist);
        }
        {
            boolean exist = dao.uniqueFieldNames("username").existAny(insertUserList);
            Assert.assertTrue(exist);
        }
        {
            boolean exist = dao.uniqueFieldNames("username", "password").existAny(insertUserList);
            Assert.assertTrue(exist);
        }
    }

    private void existAll(FlowContext flowContext){
        DAO dao = flowContext.checkInstanceData(DAO.class);
        List<User> insertUserList = flowContext.checkData("insertUserList", List.class);

        {
            boolean exist = dao.existAll(insertUserList);
            Assert.assertTrue(exist);
        }
        {
            boolean exist = dao.uniqueFieldNames("username").existAll(insertUserList);
            Assert.assertTrue(exist);
        }
        {
            boolean exist = dao.uniqueFieldNames("username", "password").existAll(insertUserList);
            Assert.assertTrue(exist);
        }
    }
}
